package com.sanchunhui.psc.ui.adapter

import android.bluetooth.BluetoothDevice
import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.sanchunhui.psc.R
import com.sanchunhui.psc.bluetooth.ble.BleData
import com.sanchunhui.psc.common.BaseConstant
import com.sanchunhui.psc.data.protocol.Member
import com.sanchunhui.psc.utils.GlideUtils
import kotlinx.android.synthetic.main.item_device_list_layout.view.*
import kotlinx.android.synthetic.main.item_members.view.*
import org.jetbrains.anko.doAsync

/**
 * @Author zyx
 * @Date 2021/7/22-13:17
 * @Dscribe
 */
class DeviceListAdapter(context: Context) : BaseRecyclerViewAdapter<BleData, DeviceListAdapter.ViewHolder>(context) {

    public var connectedPosition : Int = -1

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(mContext).inflate(R.layout.item_device_list_layout, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        val model = dataList[position]
        holder.itemView.item_ble_name_tv.text = model.dev.name
//        holder.itemView.item_ble_mac_tv.text = model.dev.address
//        holder.setText(R.id.item_ble_name_tv, "名称: " + item.dev.name ?: "Null")
//            .setText(R.id.item_ble_mac_tv, "地址: " + item.dev.address)
        //                .setText(R.id.item_ble_device_tv, item.scanRecord)
            val statusTv = holder.itemView.blue_device_item_status_tv
            if (connectedPosition == position) {
                statusTv.text = "已连接"
                statusTv.setTextColor(Color.parseColor("#ff009688"))
                holder.itemView.horizontalBattery.visibility = View.VISIBLE
                holder.itemView.tv_power_number.visibility = View.VISIBLE
                holder.itemView.horizontalBattery.setPower(model.power)
                holder.itemView.tv_power_number.text = model.power.toString()
            }
//            if (model.dev.bondState == BluetoothDevice.BOND_BONDED) {
//                statusTv.text = "(已配对)"
//                statusTv.setTextColor(Color.parseColor("#ff009688"))
//            } else {
//                statusTv.text = "(未配对)"
//                statusTv.setTextColor(Color.parseColor("#ffFF5722"))
//            }

    }

    class ViewHolder(view: View) : RecyclerView.ViewHolder(view)
}